package com.foreverwin.mes.production.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.production.dto.MaterialRequisitionDto;
import com.foreverwin.mes.production.model.MaterialRequisitionSummary;
import com.foreverwin.mes.production.service.MaterialRequisitionSummaryService;
import com.foreverwin.mes.production.vo.ShopOrderTaskVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Ervin Chen
 * @since 2020-02-24
 */
@Controller
@RequestMapping("/materialRequisitionSummarys")
public class MaterialRequisitionSummaryController {
    private final Logger logger = LoggerFactory.getLogger(MaterialRequisitionSummaryController.class);

    @Autowired
    public MaterialRequisitionSummaryService materialRequisitionSummaryService;

    /**
     * 根据id查询
     *
     * @param id 主键
     * @return
     */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getMaterialRequisitionSummaryById(@PathVariable String id) {
        MaterialRequisitionSummary result;
        try {
            result = materialRequisitionSummaryService.selectById(id);
        } catch (Exception e) {
            logger.error("getMaterialRequisitionSummaryById -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getMaterialRequisitionSummaryList(MaterialRequisitionSummary materialRequisitionSummary) {
        List<MaterialRequisitionSummary> result;
        try {
            EntityWrapper<MaterialRequisitionSummary> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(materialRequisitionSummary);
            result = materialRequisitionSummaryService.selectList(EntityWrapper);
        } catch (Exception e) {
            logger.error("getMaterialRequisitionSummaryList -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage 分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getMaterialRequisitionSummaryPageList")
    public AjaxResult getMaterialRequisitionSummaryPageList(FrontPage<MaterialRequisitionSummary> frontPage, MaterialRequisitionSummary materialRequisitionSummary) {
        Page result;
        try {
            String site = CommonMethods.getSite();
            materialRequisitionSummary.setSite(site);
            EntityWrapper<MaterialRequisitionSummary> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(materialRequisitionSummary);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                //TODO modify global query
                EntityWrapper
                        .like("handle", frontPage.getGlobalQuery())
                        .or().like("site", frontPage.getGlobalQuery())
                        .or().like("shopOrder", frontPage.getGlobalQuery())
                        .or().like("taskNo", frontPage.getGlobalQuery())
                        .or().like("itemBo", frontPage.getGlobalQuery())
                        .or().like("unitOfMeasure", frontPage.getGlobalQuery())
                        .or().like("createUserBo", frontPage.getGlobalQuery())
                        .or().like("updateUserBo", frontPage.getGlobalQuery())
                ;
            }
            result = materialRequisitionSummaryService.selectPage(frontPage.getPagePlus(), EntityWrapper);
        } catch (Exception e) {
            logger.error("getMaterialRequisitionSummaryPageList -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 保存和修改公用
     *
     * @param materialRequisitionSummary 传递的实体
     * @return null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult materialRequisitionSummarySave(@RequestBody MaterialRequisitionSummary materialRequisitionSummary) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        int count;
        try {
            count = materialRequisitionSummaryService.insertOrUpdate(materialRequisitionSummary) ? 1 : 0;
        } catch (Exception e) {
            logger.error("materialRequisitionSummarySave -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(count == 0 ? null : materialRequisitionSummary);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public AjaxResult materialRequisitionSummaryDelete(@PathVariable("id") String id) {
        int count;
        try {
            count = materialRequisitionSummaryService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("materialRequisitionSummaryDelete -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     *
     * @param ids 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult removeByIds(List<String> ids) {
        int count;
        try {
            count = materialRequisitionSummaryService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("materialRequisitionSummaryBatchDelete -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 查询所有工单任务数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/shopOrderTasks")
    public AjaxResult getShopOrderTaskList(MaterialRequisitionSummary materialRequisitionSummary) {
        List<ShopOrderTaskVo> result;
        try {
            result = materialRequisitionSummaryService.selectShopOrderTaskList();
        } catch (Exception e) {
            logger.error("getShopOrderTaskList -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有工单任务数据
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/materialRequisitions")
    public AjaxResult getMaterialRequisitionList(@RequestBody List<MaterialRequisitionDto> materialRequisitionDtos) {
        List<MaterialRequisitionDto> result;
        try {
            result = materialRequisitionSummaryService.selectMaterialRequisitionList(materialRequisitionDtos);
        } catch (Exception e) {
            logger.error("getMaterialRequisitionList -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(result);
    }
}